import axios from "axios";
import Qs from "qs";
import store from "../store";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
// TODO 打包发布需要修改
let httpConfig = {
  contextPath: "/dawanqu",
  // contextPath: "/dwq3",

  ipAddr: "www.octoplusmedia.net",

  // ipAddr: "192.168.31.202",
  // port: 8085
  port: 443
};
httpConfig.home =
  "https://" +
  httpConfig.ipAddr +
  (httpConfig.port == 80 ? "" : ":" + httpConfig.port);
// httpConfig.home =
//   "http://" +
//   httpConfig.ipAddr +
//   (httpConfig.port == 80 ? "" : ":" + httpConfig.port);
httpConfig.HOST = httpConfig.home + httpConfig.contextPath;

axios.defaults.baseURL = httpConfig.HOST;
axios.defaults.withCredentials = false;

export default {
  config: httpConfig,

  // get请求封装
  axGet(option) {
    // store.commit('SERVER_PASS',true);
    NProgress.start();
    return new Promise((resolve, reject) => {
      axios
        .get(option.url, {
          params: option.data
        })
        .then(response => {
          NProgress.done();
          // store.commit('SERVER_PASS',false);
          resolve(response.data);
        })
        .catch(err => {
          NProgress.done();
          // store.commit('SERVER_PASS',false);
          reject(err);
        });
    });
  },
  // 创建一个axios实例,post json请求
  axPost(option) {
    NProgress.start();
    return new Promise((resolve, reject) => {
      var axios_instance = axios.create({
        transformRequest: [
          data => {
            data = Qs.stringify(data);
            return data;
          }
        ],
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
        }
      });
      axios_instance
        .post(option.url, option.data)
        .then(res => {
          NProgress.done();
          if (res.data.success === false) {
            resolve(res.data);
          } else {
            resolve(res.data);
          }
        })
        .catch(error => {
          NProgress.done();
          reject(error);
        });
    });
  },
  // 文件/图片上传请求封装
  axUpload(option) {
    NProgress.start();
    return new Promise((resolve, reject) => {
      axios
        .post(option.url, option.data)
        .then(response => {
          NProgress.done();
          resolve(response.data);
        })
        .catch(err => {
          NProgress.done();
          reject(err);
        });
    });
  },
  // 文件下载请求封装
  axDownload(option) {
    NProgress.start();
    return new Promise((resolve, reject) => {
      var axios_instance = axios.create({
        transformRequest: [
          data => {
            data = Qs.stringify(data);
            return data;
          }
        ],
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
        }
      });
      axios_instance
        .post(option.url, option.data, {
          responseType: "blob"
        })
        .then(response => {
          if (!response) {
            return;
          }
          let url = window.URL.createObjectURL(response.data);
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          let ct = response.headers["content-type"];
          let fileName = "商家订单列表.xlsx";
          link.setAttribute("download", fileName);
          document.body.appendChild(link);
          link.click();
          NProgress.done();
          resolve(response.data);
        })
        .catch(err => {
          NProgress.done();
          reject(err);
        });
    });
  }
};
